<?php


namespace App\Modules\System\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class getVisitorListCriteria extends BaseCriterias
{
    private $params;


    public function __construct($storeId,$startDate,$endDate,$isLimit=false)
    {
        $this->params = [
            'store_id' => $storeId,
            'start_date' => $startDate,
            'end_date' =>$endDate,
            'is_limit' => $isLimit
        ];
    }

    public function apply($model, RepositoryInterface $repository)
    {
      $model =  $this->params['is_limit'] ?

        $model->with('member:id,name,face')
            ->where('store_id', '=', $this->params['store_id'])
            ->whereDate('created_at', '>=', $this->params['end_date'])
            ->whereDate('created_at', '<=', $this->params['start_date'])
            ->orderByDesc('created_at')->take(5)
        :
            $model->with('member:id,name,face')
                ->where('store_id', '=', $this->params['store_id'])
                ->whereDate('created_at', '>=', $this->params['end_date'])
                ->whereDate('created_at', '<=', $this->params['start_date'])
                ->orderByDesc('created_at')
        ;
      return $model;
    }


}
